<template>
  <div class="content">
    <!-- 新增课期 -->
    <el-row>
      <el-col :span="10">
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="主讲老师" prop="teacher">
              <el-select
                v-model="ruleForm.teacher"
                @change="getTeacherClass"
                placeholder="请选择主讲老师"
              >
                <el-option
                  v-for="(item, index) in teacherArr"
                  :key="index"
                  :label="labelSet(item.real_name, item.nick_name)"
                  :value="item.sys_user_id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="设置期次" prop="period_num">
              <el-input
                v-model="ruleForm.period_num"
                placeholder="请设置期次"
              ></el-input>
            </el-form-item>
            <el-form-item label="课节总数">
              <!-- <el-input v-model="$route.query.lesson_num" disabled></el-input> -->
              <el-input v-model="lesson_num" disabled></el-input>
            </el-form-item>
            <el-form-item label="课程规律" prop="law">
              <el-select
                v-model="ruleForm.law"
                @change="lawChange"
                placeholder="请选择课程规律"
              >
                <el-option
                  v-for="(item, index) in weekData.inWeek"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <!--  -->
            <el-form-item
              label
              v-for="(item, index) in ruleForm.law"
              :key="index"
            >
              <div class="u-in-line-box">
                <el-col :span="6">
                  <el-form-item>
                    <el-select
                      @change="changeOfWeek"
                      v-model="ruleForm.lawarr[index].day_of_Week"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="(item, index) in weekData.days"
                        :key="index"
                        :label="item.label"
                        :value="item.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="1">&nbsp;&nbsp;-</el-col>
                <el-col :span="8">
                  <el-form-item>
                    <!-- <el-time-select
                      placeholder="起始时间"
                      class="u-time-select"
                      value-format="HH:mm:ss"
                      v-model="ruleForm.lawarr[index].start_time"
                      :picker-options="{
                    start: '06:00',
                    step: '00:15',
                    end: '22:00'
                  }"
                    ></el-time-select>-->
                    <el-time-picker
                      :default-value="new Date('2020-01-01 10:00:00')"
                      :editable="false"
                      value-format="HH:mm:ss"
                      placeholder="开始时间"
                      v-model="ruleForm.lawarr[index].start_time"
                      style="width: 100%;"
                      @change="onTimeChange($event, index)"
                    ></el-time-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="1">&nbsp;&nbsp;-</el-col>
                <el-col :span="8">
                  <el-form-item>
                    <!-- <el-time-select
                      class="u-time-select"
                      placeholder="结束时间"
                      value-format="HH:mm:ss"
                      v-model="ruleForm.lawarr[index].end_time"
                      :picker-options="{
                    start: '06:00',
                    step: '00:15',
                    end: '22:00',
                    minTime: ruleForm.lawarr[index].start_time
                  }"
                    ></el-time-select>-->
                    <el-time-picker
                      :editable="false"
                      :disabled="!ruleForm.lawarr[index].start_time"
                      value-format="HH:mm:ss"
                      :placeholder="'结束时间'"
                      :default-value="
                        new Date(
                          '2020-01-01 ' + ruleForm.lawarr[index].start_time
                        )
                      "
                      v-model="ruleForm.lawarr[index].end_time"
                      :picker-options="{
                        // 判断开始时间大于结束时间时
                        selectableRange: `${
                          ruleForm.lawarr[index].start_time >=
                          ruleForm.lawarr[index].end_time
                            ? ruleForm.lawarr[index].start_time
                            : '00:00:00'
                        }-23:59:59`
                      }"
                      style="width: 100%;"
                    ></el-time-picker>
                  </el-form-item>
                </el-col>
              </div>
            </el-form-item>
            <el-form-item label="首课时间" prop="firstTime">
              <el-date-picker
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                :editable="false"
                :picker-options="pickerOptions1"
                v-model="ruleForm.firstTime"
                type="date"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="展示时间" prop="custom_date">
              <el-input
                v-model="ruleForm.custom_date"
                placeholder="请输入前端展示时间"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >生成课表</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <!-- <el-col :span="1">&nbsp;</el-col> -->
      <el-col :span="14">
        <el-calendar v-model="value">
          <template slot="dateCell" slot-scope="{ date, data }">
            <p :class="data.isSelected ? 'is-selected' : ''">
              {{
                data.day
                  .split('-')
                  .slice(1)
                  .join('-')
              }}
            </p>
            <!-- <p class="u-class-time">✔️</p> -->
            <!-- 接口获取遍历日历 -->
            <div v-if="$route.query.id">
              <p
                :class="{
                  'u-color-17B3A3': !conflictTime(
                    item,
                    data.day,
                    PeriodIdTime[data.day]
                  ),
                  'u-color-red': conflictTime(
                    item,
                    data.day,
                    PeriodIdTime[data.day]
                  )
                }"
                v-for="(item, index) in PeriodIdTime[data.day]"
                :key="index"
              >
                <el-popover placement="top" trigger="hover">
                  <el-button
                    @click.stop="changeDay(data.day, item)"
                    type="primary"
                    >调课</el-button
                  >
                  <el-button @click.stop="setSaveForm(item)" type="primary"
                    >更换回放地址</el-button
                  >
                  <a slot="reference">{{ clearStime(item.queryDate) }}</a>
                </el-popover>
              </p>
            </div>
            <!-- <div v-else> -->
            <!-- 新增遍历 -->
            <!-- <p
                @click="clickSetWeek()"
                class="u-color-17B3A3"
                v-for="(item,index) in setWeek(data.day,data,date)"
                :key="index"
            >{{clearStime(item)}}</p>-->
            <!-- </div> -->
            <!-- 老师课程遍历 -->
            <p
              class="u-color-409EFF"
              v-for="(item, index) in teacherClass[data.day]"
              :key="index"
            >
              {{ clearStime(item.queryDate) }}
            </p>
          </template>
        </el-calendar>
      </el-col>
    </el-row>
    <el-dialog title="调整上课时间" :visible.sync="dialogVisible" width="700px">
      <div>
        <el-form
          :model="changeTimeForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="当前时间">{{
            setOldChangeTime(changeTimeForm.start_time, changeTimeForm.end_time)
          }}</el-form-item>
          <el-form-item label="调整到">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptions0"
              class="u-time-select"
              v-model="changeTimeForm.newDate"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :editable="false"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateTime">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="更新回放地址" :visible.sync="urlFormShow" width="700px">
      <el-form :model="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="回放地址">
          <el-input v-model="urlForm.back_watch_url"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveUrl()">保存</el-button>
        <el-button @click="urlFormShow = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  coursePeriodInsert,
  coursePeriodUpdate,
  queryAutoSqlOne,
  adjustment,
  taskInstanceList,
  getLessonNum,
  updateLiveBackWatchUrl,
  getAllLecturer
} from '@/api/course.js'
import { cascadeQuery, queryList } from '@/api/api.js'
let setIndex = 0
// let weekStart = false
export default {
  data () {
    var validateNum = (rule, value, callback) => {
      if (!/^([1-9]\d*|[0]{1,1})$/.test(value) || value == 0) {
        callback('请输入正整数！')
      } else {
        callback()
      }
    }
    return {
      urlFormShow: false,
      urlForm: {
        back_watch_url: '',
        course_period_id: '',
        lesson_id: ''
      },
      lesson_num: '',
      canCreate: true,
      courseEndDay: '',
      weekDayFirstIndex: 0,
      weekDayFirst: 7,
      changeTimeForm: {},
      dialogVisible: false,
      PeriodIdTime: {},
      pickerOptions0: {
        disabledDate (time) {
          let _now = Date.now()
          return time.getTime() + 3600 * 1000 * 24 < _now
        }
      },
      pickerOptions1: {
        disabledDate: this.disabledDate
      },
      oldLaw: 0,
      timeDefaultShow: '10:00:00',
      teacherClass: {},
      calendarShow: false,
      setIndex: 0,
      // weekStart: false,
      teacherArr: [],
      value: new Date(),
      ruleForm: {
        teacher: '',
        term: '',
        law: '',
        firstTime: '',
        lawarr: [],
        period_num: '',
        custom_date: ''
      },
      weekData: {
        inWeek: [
          { label: '每周一次', value: 1 },
          { label: '每周二次', value: 2 },
          { label: '每周三次', value: 3 },
          { label: '每周四次', value: 4 },
          { label: '每周五次', value: 5 },
          { label: '每周六次', value: 6 },
          { label: '每周七次', value: 7 }
        ],
        days: [
          { label: '周一', value: 1 },
          { label: '周二', value: 2 },
          { label: '周三', value: 3 },
          { label: '周四', value: 4 },
          { label: '周五', value: 5 },
          { label: '周六', value: 6 },
          { label: '周日', value: 7 }
        ]
      },
      rules: {
        term: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        teacher: [
          { required: true, message: '请选择主讲老师', trigger: 'change' }
        ],
        law: [{ required: true, message: '请选择课程规律', trigger: 'change' }],
        period_num: [
          { required: true, message: '请输入期次', trigger: 'blur' },
          { validator: validateNum, trigger: 'blur' }
        ],
        date1: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        date2: [
          {
            type: 'date',
            required: true,
            message: '请选择时间',
            trigger: 'change'
          }
        ],
        firstTime: [
          { required: true, message: '请输入首课时间', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'ruleForm.firstTime' (val, oldVal) {
      setIndex = 0
      console.log(val)
    }
  },
  created () {
    // if (!this.$route.query.id) {
    //   this.getTeacher()
    //   if (this.$route.query.id) {
    //     this.getUpdateData()
    //   } else {
    //     this.ruleForm.teacher = ''
    //     this.ruleForm.term = ''
    //     this.ruleForm.law = ''
    //     this.ruleForm.firstTime = ''
    //     this.ruleForm.lawarr = []
    //     this.$refs.ruleForm.resetFields()
    //     this.teacherClass = {}
    //   }
    // }
  },
  activated () {
    // if (this.$route.query.id) {
    this.ruleForm.lawarr = []
    this.getTeacher()
    if (this.$route.query.id) {
      this.getUpdateData()
      this.getTimeByPeriodId()
    } else {
      this.ruleForm.teacher = ''
      this.ruleForm.term = ''
      this.ruleForm.law = ''
      this.ruleForm.firstTime = ''
      this.ruleForm.period_num = ''
      this.ruleForm.custom_date = ''
      this.oldLaw = 0
      this.$refs.ruleForm.resetFields()
      this.teacherClass = {}
      this.PeriodIdTime = {}
      this.canCreate = true
    }
    // }
  },
  computed: {},
  methods: {
    async saveUrl () {
      if (!this.urlForm.back_watch_url) {
        this.$message({
          message: '请填写正确回放地址',
          type: 'warning'
        })
        return
      }
      let res = await updateLiveBackWatchUrl({
        update: [
          {
            back_watch_url: this.urlForm.back_watch_url
          }
        ],
        where: [
          {
            course_period_id: this.urlForm.course_period_id,
            lesson_id: this.urlForm.lesson_id
          }
        ]
      })
      if (res[0]) {
        return
      }
      this.$message({
        message: '保存成功',
        type: 'success'
      })
      this.urlFormShow = false
      console.log(res[1])
    },
    setSaveForm (e) {
      console.log(e, 'eeeeeeeeeeeeee')
      this.urlForm.back_watch_url = ''
      this.urlForm.course_period_id = e.course_period_id
      this.urlForm.lesson_id = e.course_lesson_id // id course_lesson_id
      this.urlFormShow = true
    },
    labelSet (name, nickName) {
      if (name && nickName) {
        return name + ' / ' + nickName
      } else {
        return name
      }
    },
    changeFirstTime (e) {
      this.countWeek = this.lesson_num / this.ruleForm.lawarr.length
      console.log(this.countWeek, 'countWeek')

      // courseEndDay
    },
    changeOfWeek (e) {
      // day_of_Week
      if (this.ruleForm.firstTime) {
        this.ruleForm.firstTime = ''
      }
      this.weekDayFirst = 7
      var a = [7, 1, 2, 3, 4, 5, 6]
      let week = new Date().getDay()
      let day = a[week]
      this.ruleForm.lawarr.map((item, index) => {
        let long
        if (item.day_of_Week) {
          if (day > item.day_of_Week) {
            long = 7 - day + item.day_of_Week
          } else {
            long = item.day_of_Week - day
          }
          if (this.weekDayFirst > long) {
            this.weekDayFirst = long
            this.weekDayFirstIndex = index
          }
        }
      })
    },
    disabledDate (time) {
      let _now = Date.now()
      return time.getTime() - 3600 * 1000 * 24 * (this.weekDayFirst - 1) < _now
    },
    clickSetWeek () {
      this.$message('请生成课表之后调节课程时间')
    },
    conflictTime (item, day, list) {
      let isConflict = false
      let arr
      let itemTimeStart = item.start_time
        .substr(11, 5)
        .split(':')
        .join('')
      let itemTimeEnd = item.end_time
        .substr(11, 5)
        .split(':')
        .join('')
      if (this.teacherClass[day]) {
        arr = this.teacherClass[day]
        arr.map(item_ => {
          if (
            (item_.id !== item.id &&
              itemTimeStart >=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeStart <=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('')) ||
            (item_.id !== item.id &&
              itemTimeEnd >=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeEnd <=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('')) ||
            (item_.id !== item.id &&
              itemTimeEnd >=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeStart <=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join(''))
          ) {
            isConflict = true
          }
        })
      }
      if (list.length > 1) {
        list.map(item_ => {
          if (
            (item_.id !== item.id &&
              itemTimeStart >=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeStart <=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('')) ||
            (item_.id !== item.id &&
              itemTimeEnd >=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeEnd <=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('')) ||
            (item_.id !== item.id &&
              itemTimeEnd >=
                item_.end_time
                  .substr(11, 5)
                  .split(':')
                  .join('') &&
              itemTimeStart <=
                item_.start_time
                  .substr(11, 5)
                  .split(':')
                  .join(''))
          ) {
            isConflict = true
          }
        })
      }

      return isConflict
    },
    // 更改生成课期的时间
    async updateTime () {
      if (
        this.changeTimeForm.newDate &&
        this.changeTimeForm.newDate.length < 2
      ) {
        this.$message({
          message: '请选择调整后的时间段',
          type: 'warning'
        })
        return
      }
      let res = await adjustment({
        'vo.id': this.changeTimeForm.id,
        'vo.start_time': this.changeTimeForm.newDate[0],
        'vo.end_time': this.changeTimeForm.newDate[1]
      })
      if (res[0]) {
        return
      }
      this.$message({
        message: '修改课期时间成功',
        type: 'success'
      })
      this.dialogVisible = false
      this.getTimeByPeriodId()
    },
    setOldChangeTime (start, end) {
      if (start && end) {
        let arr = start.split(' ')[0].split('-')
        return (
          arr[0] +
          '年' +
          arr[1] +
          '月' +
          arr[2] +
          '日 ' +
          start.substr(11, 5) +
          '-' +
          end.substr(11, 5)
        )
      }
    },
    // 根据课期id获取这个课期
    async getTimeByPeriodId () {
      let res = await queryList({
        table_name: 'nice_course_period_lesson_time',
        course_period_id: this.$route.query.id
      })
      if (res[0]) {
        return
      }
      let obj = {}
      res[1].data.map(item => {
        let day = item.start_time.split(' ')[0]
        let startTime = item.start_time.split(' ')[1]
        let endTime = item.end_time.split(' ')[1]
        item.queryDate = startTime + '-' + endTime
        item.newDate = []
        obj[day] ? obj[day].push(item) : (obj[day] = [item])
      })
      this.PeriodIdTime = JSON.parse(JSON.stringify(obj))
    },
    changeDay (date, item) {
      // 打开表单
      if (item === '(首课)') {
        return
      }
      this.changeTimeForm = item
      console.log(this.changeTimeForm)

      this.dialogVisible = true
    },
    clearStime (e, type) {
      // if (!this.ruleForm.firstTime) {
      //   return
      // }
      // console.log(e)
      if (e === '(首课)') {
        return e
      }
      if (e) {
        let timeArr = e.split('-')
        return timeArr[0].substr(0, 5) + '-' + timeArr[1].substr(0, 5)
      }
    },
    onTimeChange (e, index) {
      this.ruleForm.lawarr[index].end_time = ''
      // if (this.ruleForm.lawarr[index].end_time) {
      //   e > this.ruleForm.lawarr[index].end_time ? this.ruleForm.lawarr[index].end_time = '' : ''
      // }
    },
    async getTeacherClass (e) {
      let res = await taskInstanceList({
        table_name: 'nice_course_teacher_task_instance',
        user_id: this.ruleForm.teacher
      })
      if (res[0]) {
        return
      }
      this.teacherClass = {}
      res[1].data.map(item => {
        let day = item.start_time.split(' ')[0]
        let startTime = item.start_time.split(' ')[1]
        let endTime = item.end_time.split(' ')[1]
        // if (
        //   parseFloat(item.course_id) !== parseFloat(this.$route.query.course_id)
        // ) {
        item.queryDate = startTime + '-' + endTime
        if (
          parseInt(item.course_period_id) !== parseInt(this.$route.query.id)
        ) {
          this.teacherClass[day]
            ? this.teacherClass[day].push(item)
            : (this.teacherClass[day] = [item])
        }
        // }
      })

      console.log(this.teacherClass, 'asdasdasd')
    },

    // 算排课表
    setdateInCalendar () {},
    // 日期转为周几
    setWeek (e, data, date) {
      let a = [7, 1, 2, 3, 4, 5, 6]
      let week = date.getDay()
      let day = a[week]
      if (e === this.ruleForm.firstTime.split(' ')[0]) {
        // weekStart = true
        let arr = ['(首课)']
        this.ruleForm.lawarr.map((item, index) => {
          if (day === this.ruleForm.lawarr[index].day_of_Week) {
            setIndex += 1
            arr.push(
              this.ruleForm.lawarr[index].start_time +
                '-' +
                this.ruleForm.lawarr[index].end_time
            )
          }
        })
        return arr
      } else if (
        // weekStart === true &&
        //  this.ruleForm.firstTime.split(' ')[0].split('-').join('')
        setIndex < this.lesson_num &&
        e.split('-').join('') >
          this.ruleForm.firstTime
            .split(' ')[0]
            .split('-')
            .join('')
      ) {
        let time = []
        let arys1 = []
        arys1 = e.split('-') // 日期为输入日期，格式为 2013-3-10
        let ssdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2])
        let week1 = String(ssdate.getDay())
          .replace('0', 7)
          .replace('1', 1)
          .replace('2', 2)
          .replace('3', 3)
          .replace('4', 4)
          .replace('5', 5)
          .replace('6', 6) // 就是你要的星期几 // 就是你要的星期几
        this.ruleForm.lawarr.map(item => {
          if (
            parseInt(item.day_of_Week) === parseInt(week1) &&
            setIndex < this.lesson_num
          ) {
            var year = this.value.getFullYear()
            var month =
              this.value.getMonth() + 1 < 10
                ? '0' + (this.value.getMonth() + 1)
                : this.value.getMonth() + 1
            // var date = this.value.getDate() < 10 ? '0' + this.value.getDate() : this.value.getDate()
            if (
              parseInt(year) === parseInt(e.split('-')[0]) &&
              parseInt(month) === parseInt(e.split('-')[1])
            ) {
              setIndex += 1
              time.push(item.start_time + '-' + item.end_time)
            }
          }
        })
        // console.log([time], 2)
        return time
        // console.log(time.indexOf(week1))
        // if (time.indexOf(week1) !== -1) {
        //   return this.ruleForm.lawarr[time.indexOf(week1)].start_time + '-' + this.ruleForm.lawarr[time.indexOf(week1)].end_time
        // }
      }
    },
    // 更新时获取列表
    async getUpdateData () {
      let res = await queryAutoSqlOne({
        table_name: 'nice_course_period',
        id: this.$route.query.id
      })
      if (res[0]) {
        return
      }
      let dayInWeek = JSON.parse(res[1].data.frequency).length
      this.ruleForm.teacher = res[1].data.teacher
      this.ruleForm.law = dayInWeek
      this.ruleForm.firstTime = res[1].data.first_course
      this.ruleForm.lawarr = JSON.parse(res[1].data.frequency)
      this.ruleForm.period_num = res[1].data.sort
      this.oldLaw = this.ruleForm.lawarr.length
      this.ruleForm.custom_date = res[1].data.custom_date
      this.getTeacherClass(this.ruleForm.teacher)
    },

    lawChange (e) {
      this.ruleForm.firstTime = ''
      // let arr = []
      // for (let i = 0; i < e; i++) {
      //   arr.push({ day_of_Week: '', start_time: '', end_time: '' })
      // }
      // this.ruleForm.lawarr = arr
      if (this.ruleForm.lawarr.length > e) {
        // for (let i = e; i >= e; i--) {
        this.ruleForm.lawarr = this.ruleForm.lawarr.splice(0, e)
        // }
      } else {
        for (let i = this.ruleForm.lawarr.length; i < e; i++) {
          this.ruleForm.lawarr.push({
            day_of_Week: '',
            start_time: '',
            end_time: ''
          })
        }
      }
    },
    async getTeacher () {
      let res = await getAllLecturer({})
      if (res[0]) {
        return
      }
      this.teacherArr = res[1].data.list
      // 获取课期总数

      let res2 = await getLessonNum({
        'lesson.course_id': this.$route.query.course_id
      })
      if (res2[0]) {
        return
      }
      this.lesson_num = res2[1].data.lesson_num || 0
    },
    // 生成课表
    async setTable () {
      let isNone = false
      this.ruleForm.lawarr.map(item => {
        if (!item.day_of_Week || !item.start_time || !item.end_time) {
          isNone = true
        }
      })
      if (isNone) {
        this.$message({
          message: '检测到您当前未完善课程规律',
          type: 'warning'
        })
        return
      }
      if (!this.canCreate) {
        // this.$message({
        //   message: '请勿重复生成课表',
        //   type: 'warning'
        // })
        return
      }
      if (!this.$route.query.id && this.canCreate) {
        this.canCreate = false
        // 新建
        let res = await coursePeriodInsert({
          // 'nice_course_period.code': 'K00008801',
          'nice_course_period.course_id': this.$route.query.course_id,
          'vo.times': this.lesson_num,
          'vo.first_course': this.ruleForm.firstTime,
          'vo.frequency': JSON.stringify(this.ruleForm.lawarr),
          'nice_course_period.sort': parseInt(this.ruleForm.period_num),
          'nice_course_period.custom_date': parseInt(this.ruleForm.custom_date),
          'nice_course_period.teacher': this.ruleForm.teacher,
          'nice_course_period.frequency': JSON.stringify(this.ruleForm.lawarr),
          'nice_course_period.first_course': this.ruleForm.firstTime
        })
        if (res[0]) {
          return
        }
        this.$message({
          message: '生成课表成功',
          type: 'success'
        })
        // this.$router.go(-1)
        let courseId = this.$route.query.course_id
        // let periodNum = this.ruleForm.period_num
        let lessonNum = this.lesson_num
        this.canCreate = true
        // this.ruleForm.lawarr = []
        this.$router.push({
          name: 'add-update-course-term',
          query: {
            id: res[1].data.course_period_id,
            course_id: courseId,
            // period_num: periodNum,
            lesson_num: lessonNum
          }
        })
        this.getTeacher()
        this.getUpdateData()
        this.getTimeByPeriodId()
      } else {
        // 更新
        this.$confirm(
          '此操作将根据课程规律重新生成课程日历, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(async () => {
            let res = await coursePeriodUpdate({
              // 'nice_course_period.code': 'K00008821',
              'nice_course_period.course_id': this.$route.query.course_id,
              'vo.times': this.lesson_num,
              'vo.first_course': this.ruleForm.firstTime,
              'vo.frequency': JSON.stringify(this.ruleForm.lawarr),
              'nice_course_period.id': this.$route.query.id,
              'nice_course_period.sort': this.ruleForm.period_num,
              'nice_course_period.custom_date': this.ruleForm.custom_date,
              'nice_course_period.teacher': this.ruleForm.teacher,
              'nice_course_period.frequency': JSON.stringify(
                this.ruleForm.lawarr
              ),
              'nice_course_period.first_course': this.ruleForm.firstTime
            })
            //
            if (res[0]) {
              return
            }
            this.$message({
              message: '保存课表成功',
              type: 'success'
            })
            // this.$router.go(-1)
            this.getTimeByPeriodId()
          })
          .catch(() => {})
      }
    },

    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.setTable()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.u-in-line-box {
  /* margin-top: 20px; */
}
.u-class-time {
  background: red;
}
.el-calendar-day p {
  line-height: 10px;
}
.u-color-17B3A3 {
  color: #3e83f8;
  font-size: 12px;
  /* line-height: 10px; */
}

.u-color-17B3A3 a {
  text-decoration: underline;
}
.u-color-409EFF {
  color: #c0c4cc;
  font-size: 12px;
}
.content {
  /* min-width: 1600px;
  overflow: auto; */
}
.content >>> .el-calendar-day {
  height: 110px;
}
.u-time-select {
  width: 100%;
}
.u-color-red a {
  color: red;
  font-size: 12px;
  text-decoration: underline;
}
.u-seva-url {
  margin-top: 100px;
}
.u-url-title {
  font-size: 15px;
  padding-left: 100px;
  margin-bottom: 20px;
}
</style>
